package com.ruoyi.product.service.impl;

import com.ruoyi.product.domain.Category;
import com.ruoyi.product.mapper.CategoryMapper;
import com.ruoyi.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 商品三级分类Service业务层处理
 *
 * @author syl
 * @date 2025-08-11
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询商品三级分类
     *
     * @param catId 商品三级分类主键
     * @return 商品三级分类
     */
    @Override
    public Category selectCategoryByCatId(Long catId) {
        return categoryMapper.selectCategoryByCatId(catId);
    }

    /**
     * 查询商品三级分类列表
     *
     * @param category 商品三级分类
     * @return 商品三级分类
     */
    @Override
    public List<Category> selectCategoryList(Category category) {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增商品三级分类
     *
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category) {
        // 判断是否分类已存在
        Category categoryByName = categoryMapper.selectCategoryByName(category.getName());
        if (categoryByName != null) {
            throw new RuntimeException("当前分类已存在");
        }

        // 判断当前层级是否正确
        Long parentCid = category.getParentCid();
        if (parentCid == 0 && category.getCatLevel() != 1) {
            throw new RuntimeException("当前层级错误");
        }
        if (parentCid != 0) {
            Category parentCategory = categoryMapper.selectCategoryByCatId(parentCid);
            if (category.getCatLevel() - parentCategory.getCatLevel() != 1) {
                throw new RuntimeException("当前层级错误");
            }
        }
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改商品三级分类
     *
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除商品三级分类
     *
     * @param catIds 需要删除的商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatIds(Long[] catIds) {
        return categoryMapper.deleteCategoryByCatIds(catIds);
    }

    /**
     * 删除商品三级分类信息
     *
     * @param catId 商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatId(Long catId) {
        return categoryMapper.deleteCategoryByCatId(catId);
    }

    @Override
    public long[] treeCatId(Long catId) {
        ArrayList<Long> catPathIds = new ArrayList<>();
        // 获取当前分类
        Category category = categoryMapper.selectCategoryByCatId(catId);
        long baseCatId = category.getCatId();
        long parentCatId = category.getParentCid();
        catPathIds.add(baseCatId);
        // 获取当前分类的父分类
        getParentCatId(parentCatId, catPathIds);
        Collections.reverse(catPathIds);
        return catPathIds.stream().mapToLong(Long::longValue).toArray();
    }

    /**
     * 递归获取当前分类的父分类
     */
    private void getParentCatId(long parentCatId, ArrayList<Long> catPathIds) {
        Category category = categoryMapper.selectCategoryByCatId(parentCatId);
        if (category != null) {
            catPathIds.add(category.getCatId());
            getParentCatId(category.getParentCid(), catPathIds);
        }
    }
}
